<!--
 * @Description: 登录页面
 * @Author: zhihong deng
 * @Date: 2021-07-28 20:11:15
 * @LastEditors: zhihong deng
 * @LastEditTime: 2021-07-28 22:36:13
-->
<template>
  <div class="login-box">
    <div class="login-form">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="login" :label-position="labelPosition">
        <h2>用户登录</h2>
        <el-form-item label="用户名" prop="username">
          <el-input type="text" v-model="ruleForm.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="submitButton" type="primary" @click="submitForm('ruleForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </div>

  </div>
</template>

<script>
import {login} from "../../api/back"
export default {
  data () {
    return {
      labelPosition: 'top',
      ruleForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
        ]
      }
    }
  },
  methods: {
    submitForm (params) {
       this.$refs[params].validate((valid) => {
         console.log('valid',valid);
          if (!valid) {
             this.$message.error('请填写正确信息');
              return false;
          }else{
            console.log(this.ruleForm);
            login(this.ruleForm).then( res => {
              console.log(res.data);
              let {meta,data} = res.data
              if(meta.status == 200){
                // 1.验证成功，记录成功登录的token  其他的页面都需要基于token做验证
                localStorage.setItem('cmsToken',data.token)
                // 2.前往后台的主页
                this.$router.push('/')
                this.$message.success(meta.msg);
                return false;
              }else{
                this.$message.error(meta.msg);
                return false;
              }
            }).catch(error => {
              this.$message.error('请填写正确信息');
              return false;
            })
          }
        });
    }
  }
}
</script>

<style lang="less" scope>
  html{
    width: 100%;
    height: 100%;
    background: #354150;
  }
  .login-box{
    width: 500px;
    height: 400px;
    background: #ffffff;
    position:absolute;
    top:0;
    bottom:0;
    left:0;
    right:0;
    margin:auto;
    border-radius: 5px;
    .login-form{
      display: flex;
      justify-content: center;
      align-items: center;
      width: 100%;
      height: 100%;
      .login{
        width: 100%;
        height: 100%;
        padding: 5% 10%;
        box-sizing: border-box;
      }
      .submitButton{
        width: 100%;
      }
    }
  }
</style>